﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notepad
{
    public partial class Form1 : Form
    {

        bool b = false;//判断文件是否为新建的
        bool s = true;//判断文件是否保存
        public Form1()
        {
            InitializeComponent();
        }
        //保存函数
        public void baochun(string name)
        {
            FileStream sd = null;
            try
            {
                sd = new FileStream(name, FileMode.Truncate, FileAccess.Write);
                string v = rtxtNotepad.Text;
                byte[] buf = Encoding.Default.GetBytes(v);
                sd.Write(buf, 0, buf.Length); 
            }
            catch (Exception ex)
            {
                Trace.Write("清空日志文件失败：" + ex.Message);
                
            }
            finally
            {
                if (sd != null)
                {
                    sd.Close();
                    sd.Dispose();
                }
            }
        } 
        //打开文件函数
        public void dakai(string name)
        {
            FileStream sd = null;
            
            try
            {
                sd = new FileStream(name, FileMode.OpenOrCreate, FileAccess.Read);
                this.Text = name;
                byte[] buf = new byte[1024 * 1024];
                rtxtNotepad.Clear();
                int r;
                do
                {
                r = sd.Read(buf, 0, buf.Length);
                string v = Encoding.Default.GetString(buf, 0, r);
                rtxtNotepad.Text += v;//打开代码语句
                 } while (r != 0);
            }
            catch(Exception ex)
            {
                Trace.Write("打开文件失败：" + ex.Message);
            }
            finally
            {
                if (sd != null)
                {
                sd.Close();
                sd.Dispose();
                }
            }
            
            
        }
        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void rtxtNotepad_TextChanged(object sender, EventArgs e)
        {
            // 文本被修改后，设置s为false，表示文件未保存
            s = false;

        }
        //新建
        private void tsmiNew_Click(object sender, EventArgs e)
        {
            if (b == true || rtxtNotepad.Text.Trim() != "")
            {
                //文件未保存
                if (s == false)
                {
                    string result
                     = MessageBox.Show("文件尚未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString();               
                  switch (result)
                 {
                    case "Yes":
                            //是打开的磁盘文件
                        if (b == true)
                        {
                                baochun(odlgNotepad.FileName);
                            
                        }
                        else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
                            {
                                baochun(sdlgNotepad.FileName);
                            }
                            s = true;
                            break;
                        case "No":
                            b =false;      
                            break;
                    }   
                }
            }
            rtxtNotepad.Clear();
            this.Text = "记事本";
        }
        //打开
        private void tsmiOpen_Click(object sender, EventArgs e)
        {
            if (b == true || rtxtNotepad.Text.Trim() != "")
            {
                if (s == false)
                {
                    string result
                    = MessageBox.Show("文件尚未保存,是否保存?",
                        "保存文件", MessageBoxButtons.YesNoCancel).ToString();
                    switch (result)
                    {
                        case "Yes":
                            if (b == true)
                            {
                                baochun(odlgNotepad.FileName);
                            }
                            else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
                            {
                                baochun(sdlgNotepad.FileName);
                            }
                            s = true;
                            break;
                        case "No":
                            b = false;
                            rtxtNotepad.Text = "";
                            break;
                    }
                }
            }
            odlgNotepad.RestoreDirectory = true;
            if ((odlgNotepad.ShowDialog() == DialogResult.OK) && odlgNotepad.FileName != "")
            {
                dakai(odlgNotepad.FileName);
                b = true;
            }
            s = true;
        }
        //保存
        private void tsmiSave_Click(object sender, EventArgs e)
        {
            // 若文件从磁盘打开并且修改了其内容
            if (b == true && rtxtNotepad.Modified == true)
            {             
                s = true;
            }
            else if (b == false && rtxtNotepad.Text.Trim() != "" &&
                sdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                // RichTextBoxStreamType.PlainText           
                s = true;
                b = true;
                odlgNotepad.FileName = sdlgNotepad.FileName;
            }
            baochun(odlgNotepad.FileName);
            this.Text = odlgNotepad.FileName;
        }

        private void tsmiSaveA_Click(object sender, EventArgs e)
        {
            if (sdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                FileStream sd =
                    sd = new FileStream(sdlgNotepad.FileName, FileMode.OpenOrCreate, FileAccess.Write);
                    string v = rtxtNotepad.Text;
                    byte[] buf = Encoding.Default.GetBytes(v);
                    sd.Write(buf, 0, buf.Length);
                    sd.Close();
                    sd.Dispose();
                s = true;
            }

        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 撤销UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Undo();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Copy();
        }

        private void 剪切XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Cut();
        }

        private void 粘贴VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Paste();
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtNotepad.SelectAll();
        }

        private void 日期DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtxtNotepad.AppendText(System.DateTime.Now.ToString());
        }

        private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (自动换行WToolStripMenuItem.Checked == false)
            {
                自动换行WToolStripMenuItem.Checked = true;            // 选中该菜单项
                rtxtNotepad.WordWrap = true;        // 设置为自动换行
            }
            else
            {
                自动换行WToolStripMenuItem.Checked = false;
                rtxtNotepad.WordWrap = false;
            }

        }

        private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fdlgNotepad.ShowColor = true;
            if (fdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                rtxtNotepad.SelectionColor = fdlgNotepad.Color;
                rtxtNotepad.SelectionFont = fdlgNotepad.Font;
            }

        }

        private void 工具栏TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Point point;
            if (工具栏TToolStripMenuItem.Checked == true)
            {
                // 隐藏工具栏时，把坐标设为（0，20）,因为菜单的高度为24
                point = new Point(0, 20);
                工具栏TToolStripMenuItem.Checked = false;
                tlsNotepad.Visible = false;
                // 设置多格式文本框左上角位置
                rtxtNotepad.Location = point;
                // 隐藏工具栏后，增加文本框高度
                rtxtNotepad.Height += tlsNotepad.Height;
            }
            else
            {
                // 显示工具栏
                point = new Point(0, 40);
                工具栏TToolStripMenuItem.Checked = true;
                tlsNotepad.Visible = true;
                rtxtNotepad.Location = point;
                rtxtNotepad.Height -= tlsNotepad.Height;
            }

        }

        private void 关于记事本AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout ob_FrmAbout = new frmAbout();
            ob_FrmAbout.Show();

        }

        private void tlsNotepad_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int n;
            // 变量n用来接收按下按钮的索引号从0开始
            n = tlsNotepad.Items.IndexOf(e.ClickedItem);
            switch (n)
            {
                case 0:
                    tsmiNew_Click(sender, e);
                    break;
                case 1:
                    tsmiOpen_Click(sender, e);
                    break;
                case 2:
                    tsmiSaveA_Click(sender, e);
                    break;
                /*case 3:
                    tsmiCopy_Click(sender, e);
                    break;*/ // 我们不用case3

                case 4:
                    剪切XToolStripMenuItem_Click(sender, e);
                    break;
                case 5:
                    复制CToolStripMenuItem_Click(sender, e);
                    break;
                
                case 6:
                    粘贴VToolStripMenuItem_Click(sender, e);
                    break;

            }

        }

        private void tmrNotepad_Tick(object sender, EventArgs e)
        {
            
            toolStripStatusLabel1.Text = System.DateTime.Now.ToString();
        }
    }
}
